package com.winkey.facility.gateway.service.parking.impl.aist;

import com.sun.jna.Native;
import com.sun.jna.NativeLong;
import com.sun.jna.Pointer;
import com.sun.jna.platform.win32.WinDef;
import com.winkey.facility.gateway.service.parking.impl.callback.NET_SMARTRECVCALLBACK;
import com.winkey.facility.gateway.service.parking.impl.callback.NET_SSP_VALIDATOR_CALLBACK;
import com.winkey.facility.gateway.service.parking.impl.jna.EYESTNETLIBLibrary;
import org.springframework.stereotype.Component;

import javax.swing.*;
import java.io.UnsupportedEncodingException;

/**
 * @author Administrator
 */
@Component
public class Camera {
    public Camera(){
        cameraInit();
    }
    static NET_SMARTRECVCALLBACK PlateCallBack = new NET_SMARTRECVCALLBACK();
    static NET_SSP_VALIDATOR_CALLBACK SspValidatorCallBack = new NET_SSP_VALIDATOR_CALLBACK();

    public void cameraInit() {
        EYESTNETLIBLibrary.INSTANCE.EYEST_NET_INIT();
        System.out.println("init camera ok");
    }

    public NativeLong login(String CamIp) {
        EYESTNETLIBLibrary.NET_CONPARAM NetConparam = new EYESTNETLIBLibrary.NET_CONPARAM();
        NetConparam.AutoReconnect = 1;
        NetConparam.IpAddr.IpV4 = CamIp.getBytes();
        NetConparam.Port = 8088;
        NativeLong logid = EYESTNETLIBLibrary.INSTANCE.EYEST_NET_LOGIN(NetConparam);
        if (logid.longValue() > 0) {
            System.out.println("登录成功：logid = " + logid);
            //设置识别回调函数
            EYESTNETLIBLibrary.INSTANCE.EYEST_REGISTER_RESULT_CALLBACK(logid, PlateCallBack, Pointer.NULL);
            EYESTNETLIBLibrary.INSTANCE.EYEST_REGISTER_SSP_VALIDATOR_CALLBACK(logid, SspValidatorCallBack);
        }
        return logid;
    }

    public String logout(NativeLong logid) {
        //退出登录
        System.out.println("退出登录");
        EYESTNETLIBLibrary.INSTANCE.EYEST_NET_LOGOUT(logid);
        return "logout success";
    }

    public int restart(NativeLong loginid){
        System.out.println("重启设备："+loginid);
        return EYESTNETLIBLibrary.INSTANCE.EYEST_NET_RESTART(loginid);
    }

    public void playVideo(NativeLong logid, JFrame Video) {
        //播放视频
        System.out.println("播放视频");
        Video.setBounds(500, 250, 800, 600);
        Video.setVisible(true);
        Pointer p = Native.getComponentPointer(Video);
        WinDef.HWND hwnd = new WinDef.HWND(p);
        EYESTNETLIBLibrary.NET_PREVIEWPARAM NetPreviewParam = new EYESTNETLIBLibrary.NET_PREVIEWPARAM();
        NetPreviewParam.AutoReconnect = 1;
        NetPreviewParam.Channel = 0;
        //RTSP端口
        NetPreviewParam.VideoPort = 556;
        NetPreviewParam.StreamType = 1;
        //编码数据回调
        NetPreviewParam.DataCallback = null;
        //解码BGR 数据回调
        NetPreviewParam.DecodeCallback = null;
        // 播放窗口
        NetPreviewParam.hWnd = hwnd;
        //NetPreviewParam.UserParam = "AstTest";
        NetPreviewParam.Protol = 1;

        NativeLong playId = EYESTNETLIBLibrary.INSTANCE.EYEST_NET_START_PREVIEW(logid, NetPreviewParam);
        if (playId.longValue() > 0) {
            System.out.println("播放成功：playId = " + playId);
            //btnPlayVideo.setEnabled(false);
        }
    }


    public int SnapPic(String CamIp) {
        //手动抓图
        System.out.println("手动抓图");
        //抓拍后会以识别回调的方式收到车牌和图片 1:成功  0:失败
        return EYESTNETLIBLibrary.INSTANCE.EYEST_NET_HAND_SNAP_EX(CamIp, 37890, null);
    }


    public int openGate(String CamIp) {
        //开闸 1:成功  0:失败
        System.out.println("开闸");
        return EYESTNETLIBLibrary.INSTANCE.EYEST_NET_IO_CONTROL_EX(CamIp, 37890, 0, 0);
    }

    public int closeGate(String CamIp) {
        //开闸 1:成功  0:失败
        System.out.println("关闸");
        return EYESTNETLIBLibrary.INSTANCE.EYEST_NET_IO_CLOSE_DOOR_EX(CamIp, 37890, 0, 0);
    }

    public int setWorkMode(NativeLong loginid, byte mode) {
        System.out.println("设置工作模式");
        //1:入口 2:出口
        EYESTNETLIBLibrary.NET_DEV_WORKMODE work_cfg = new EYESTNETLIBLibrary.NET_DEV_WORKMODE();
        work_cfg.workmode = mode;
        return EYESTNETLIBLibrary.INSTANCE.EYEST_NET_SET_WORKMODE_PARAM(loginid, work_cfg);
    }


    public int setParkVip(NativeLong loginid,byte[] plate,int type,int[] startTime,int[] endTime) throws UnsupportedEncodingException {
        System.out.printf("设置白名单");
        EYESTNETLIBLibrary.WhiteListMember[] vip_cfg = new EYESTNETLIBLibrary.WhiteListMember[1];
        EYESTNETLIBLibrary.WhiteListMember member = new EYESTNETLIBLibrary.WhiteListMember();
        //type  0:白名单  1:黑名单
        member.type = type;
        member.szplate = plate;
        member.nStartTime = startTime;
        return EYESTNETLIBLibrary.INSTANCE.EYEST_NET_SET_PARK_VIP_PARAM(loginid,vip_cfg,vip_cfg.length);
    }
}

